package com.kensure.shike.user.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import co.kensure.frame.JSBaseService;
import co.kensure.mem.ArithmeticUtils;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.DateUtils;
import co.kensure.mem.MapUtils;
import co.kensure.mem.NumberUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.baobei.model.SKBaobei;
import com.kensure.shike.baobei.service.SKBaobeiService;
import com.kensure.shike.bsc.model.SKBanShiChu;
import com.kensure.shike.bsc.service.SKBanShiChuService;
import com.kensure.shike.changshang.model.ChangShang;
import com.kensure.shike.changshang.service.ChangShangService;
import com.kensure.shike.chongzhi.model.SKUserInout;
import com.kensure.shike.order.model.SKFinPay;
import com.kensure.shike.order.model.SKOrder;
import com.kensure.shike.order.service.SKFinPayService;
import com.kensure.shike.order.service.SKOrderService;
import com.kensure.shike.user.dao.SKUserFRDao;
import com.kensure.shike.user.model.SKRole;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.model.SKUserChanChu;
import com.kensure.shike.user.model.SKUserFR;
import com.kensure.shike.user.model.SKUserFRDetail;
import com.kensure.shike.user.model.SKUserShouYi;
import com.kensure.shike.zhang.model.SKUserZhang;
import com.kensure.shike.zhang.service.SKUserZhangService;

/**
 * 会员分润情况服务实现类
 * 
 * @author fankd created on 2019-6-24
 * @since
 */
@Service
public class SKUserFRService extends JSBaseService {

	@Resource
	private SKUserFRDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKUserService sKUserService;
	@Resource
	private SKUserZhangService sKUserZhangService;
	@Resource
	private SKBaobeiService sKBaobeiService;
	@Resource
	private SKUserFRDetailService sKUserFRDetailService;
	@Resource
	private SKOrderService sKOrderService;
	@Resource
	private SKFinPayService sKFinPayService;
	@Resource
	private ChangShangService changShangService;
	@Resource
	private SKBanShiChuService sKBanShiChuService;

	public SKUserFR selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKUserFR> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<SKUserFR> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKUserFR obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_user_fr"));
		return dao.insert(obj);
	}

	public boolean update(SKUserFR obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	/**
	 * 根据订单获取分润情况
	 * 
	 * @param orderid
	 * @param paytype
	 * @return
	 */
	public SKUserFR selectByOrderid(Long orderid, String paytype) {
		Map<String, Object> parameters = MapUtils.genMap("orderid", orderid, "paytype", paytype);
		List<SKUserFR> list = selectByWhere(parameters);
		SKUserFR fr = null;
		if (CollectionUtils.isNotEmpty(list)) {
			fr = list.get(0);
			SKUser user = sKUserService.selectOne(fr.getUserid());
			fr.setUserName(user.getNickname());
		}
		return fr;
	}

	/**
	 * vip会员分润
	 * 
	 * @param userid
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void addVipFr(SKUserInout order) {
		SKUserZhang one = vipOne(order.getUserid(), order.getId());
		SKUserZhang two = vipTwo(order.getUserid(), order.getId());

		SKUserFR fr = new SKUserFR();
		fr.setUserid(order.getUserid());
		fr.setJine(order.getJine());
		fr.setStatus(0);
		fr.setPaytype("VIP");
		fr.setOrderid(order.getId() + "");
		Date timeStart = new Date();

		fr.setTimeStart(timeStart);
		fr.setTimeExpire(timeStart);
		if (one != null) {
			fr.setFruserid1(one.getUserid());
			fr.setFrjine1(one.getYue());
		}
		if (two != null) {
			fr.setFruserid2(two.getUserid());
			fr.setFrjine2(two.getYue());
		}
		insert(fr);
	}

	/**
	 * 会员一级分润 类型是109
	 */
	private SKUserZhang vipOne(long fromuserid, long orderid) {
		SKUser fromuser = sKUserService.selectOne(fromuserid);
		if (fromuser.getRefereeId() == null) {
			return null;
		}
		SKUser touser = sKUserService.selectOne(fromuser.getRefereeId());
		// 如果一级推荐人不是会员，终止
		if (touser == null || "0000".equalsIgnoreCase(touser.getLevelCode())) {
			return null;
		}
		String levelCode = touser.getLevelCode();
		SKRole role = SKRoleService.selectRoleByCode(levelCode);
		if (role.getVip1() > 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(orderid);
			obj.setBusitypeid(109);
			obj.setYue(role.getVip1());
			obj.setUserid(touser.getId());
			obj.setRemark(fromuser.getNickname() + "的会员分润");
			sKUserZhangService.add(obj);
			return obj;
		}
		return null;
	}

	/**
	 * 会员二级分润 类型是110
	 */
	private SKUserZhang vipTwo(long fromuserid, long orderid) {
		SKUser fromuser = sKUserService.selectOne(fromuserid);
		if (fromuser.getRefereeId() == null) {
			return null;
		}
		SKUser erjiuser = sKUserService.selectOne(fromuser.getRefereeId());
		// 如果一级推荐人不是会员，终止
		if (erjiuser == null || erjiuser.getRefereeId() == null || "0000".equalsIgnoreCase(erjiuser.getLevelCode())) {
			return null;
		}

		SKUser touser = sKUserService.selectOne(erjiuser.getRefereeId());
		// 如果二级推荐人不是会员，终止
		if (touser == null || "0000".equalsIgnoreCase(touser.getLevelCode())) {
			return null;
		}
		String levelCode = touser.getLevelCode();
		SKRole role = SKRoleService.selectRoleByCode(levelCode);
		if (role.getVip1() > 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(orderid);
			obj.setBusitypeid(110);
			obj.setYue(role.getVip2());
			obj.setUserid(touser.getId());
			obj.setRemark(erjiuser.getNickname() + "的会员分润");
			sKUserZhangService.add(obj);
			return obj;
		}
		return null;
	}

	/**
	 * 销售分润，预先计算
	 * 
	 * @param userid
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void addFinpayFr(SKFinPay finpay) {
		List<SKOrder> orderlist = sKOrderService.selectByFinpay(finpay.getId());
		List<SKUserFRDetail> detailList = new ArrayList<>();
		for (SKOrder order : orderlist) {
			SKUserFRDetail detail = orderYuSuan(order);
			sKUserFRDetailService.insert(detail);
			detailList.add(detail);
		}
		SKUserFR fr = new SKUserFR();
		fr.setUserid(finpay.getUserid());
		fr.setStatus(0);
		fr.setPaytype("DD");
		fr.setOrderid(finpay.getId() + "");
		Double fenrun = 0D;
		Double frjine1 = 0D;
		Double frjine2 = 0D;
		Double bscjine1 = 0D;
		Double bscjine2 = 0D;
		Double tjrjine1 = 0D;
		Double tjrjine2 = 0D;
		for (SKUserFRDetail detail : detailList) {
			fenrun = ArithmeticUtils.add(fenrun, detail.getJine());
			if (detail.getFruserid1() != null) {
				fr.setFruserid1(detail.getFruserid1());
			}
			if (detail.getFruserid2() != null) {
				fr.setFruserid2(detail.getFruserid2());
			}
			if (detail.getBscid1() != null) {
				fr.setBscid1(detail.getBscid1());
			}
			if (detail.getBscid2() != null) {
				fr.setBscid2(detail.getBscid2());
			}
			if (detail.getFrjine1() != null) {
				frjine1 = ArithmeticUtils.add(frjine1, detail.getFrjine1());
			}
			if (detail.getFrjine2() != null) {
				frjine2 = ArithmeticUtils.add(frjine2, detail.getFrjine2());
			}
			if (detail.getBscjine1() != null) {
				bscjine1 = ArithmeticUtils.add(bscjine1, detail.getBscjine1());
			}
			if (detail.getBscjine2() != null) {
				bscjine2 = ArithmeticUtils.add(bscjine2, detail.getBscjine2());
			}
			if (detail.getTjr1() != null) {
				fr.setTjr1(detail.getTjr1());
			}
			if (detail.getTjrjine1() != null) {
				tjrjine1 = ArithmeticUtils.add(tjrjine1, detail.getTjrjine1());
			}
			if (detail.getTjr2() != null) {
				fr.setTjr2(detail.getTjr2());
			}
			if (detail.getTjrjine2() != null) {
				tjrjine2 = ArithmeticUtils.add(tjrjine2, detail.getTjrjine2());
			}
		}
		fr.setJine(fenrun);
		fr.setFrjine1(frjine1);
		fr.setFrjine2(frjine2);
		fr.setBscjine1(bscjine1);
		fr.setBscjine2(bscjine2);
		fr.setTjrjine1(tjrjine1);
		fr.setTjrjine2(tjrjine2);
		
		Date timeStart = new Date();
		fr.setTimeStart(timeStart);
		fr.setTimeExpire(timeStart);

		// 微信手续费
		Double shouxufei = 0D;
		if (fr.getJine() > 0) {
			shouxufei = ArithmeticUtils.mul(0.006D, fr.getJine(), 2);
			if (shouxufei == 0) {
				shouxufei = 0.01D;
			}
		}
		fr.setSxf(shouxufei);
		// 设置利润
		setLirun(fr);

		insert(fr);
	}

	/**
	 * 计算利润
	 * 
	 * @param fr
	 */
	private void setLirun(SKUserFR fr) {
		double lirun = ArithmeticUtils.subAll(fr.getJine(), fr.getFrjine1(), fr.getFrjine2(), fr.getBscjine1(), fr.getBscjine2(), fr.getSxf());
		fr.setLirun(lirun);
	}

	/**
	 * 销售分润预先计算
	 */
	private SKUserFRDetail orderYuSuan(SKOrder order) {
		SKUserFRDetail detail = new SKUserFRDetail();
		detail.setOrderid(order.getId());
		detail.setFinpayid(order.getFinpayid());
		detail.setUserid(order.getUserid());
		detail.setJine(order.getFenrun());
		setOneFr(order, detail);
		if (detail.getFrjine1() != null) {
			setTowFr(order, detail);
		}
		setBsc1Fr(order, detail);
		setBsc2Fr(order, detail);
		setTjr1Fr(order, detail);
		setTjr2Fr(order, detail);
		return detail;
	}

	/**
	 * 设置销售一级分润
	 */
	private void setOneFr(SKOrder order, SKUserFRDetail detail) {
		SKBaobei baobei = sKBaobeiService.selectOne(order.getBbid());
		// 发生人
		SKUser fromuser = sKUserService.selectOne(order.getUserid());
		if (fromuser.getRefereeId() == null) {
			return;
		}
		SKUser yijiuser = sKUserService.selectOne(fromuser.getRefereeId());
		// 如果一级推荐人不是会员，终止
		if (yijiuser == null) {
			return;
		}
		// 分润标准
		Double frbz = baobei.getVip1Fenrui1();
		Double frjine = ArithmeticUtils.mul(frbz, order.getFenrun(), 2);
		;
		detail.setFruserid1(yijiuser.getId());
		detail.setFrjine1(frjine);
	}

	/**
	 * 根据销售二级分润
	 */
	private void setTowFr(SKOrder order, SKUserFRDetail detail) {
		SKBaobei baobei = sKBaobeiService.selectOne(order.getBbid());
		// 发生人
		SKUser fromuser = sKUserService.selectOne(order.getUserid());
		SKUser erjiuser = null;
		if (fromuser != null) {
			SKUser yijiuser = sKUserService.selectOne(fromuser.getRefereeId());
			// 如果一级推荐人不是会员，终止
			if (yijiuser == null) {
				return;
			}
			erjiuser = sKUserService.selectOne(yijiuser.getRefereeId());
		}
		// 如果二级推荐人不是会员黄金会员及以上，终止
		if (erjiuser == null) {
			return;
		}
		// 分润标准
		Double frbz = baobei.getVip2Fenrui2();
		Double frjine = ArithmeticUtils.mul(frbz, order.getFenrun(), 2);
		detail.setFruserid2(erjiuser.getId());
		detail.setFrjine2(frjine);
	}

	/**
	 * 设置市办事处分润
	 */
	private void setBsc1Fr(SKOrder order, SKUserFRDetail detail) {
		SKFinPay finpay = sKFinPayService.selectOne(order.getFinpayid());
		if (StringUtils.isBlank(finpay.getShi())) {
			return;
		}
		SKBanShiChu bsc = sKBanShiChuService.getShi(finpay.getShi());
		if (bsc == null) {
			return;
		}
		// 分润标准
		Double frbz = bsc.getFenrun();
		Double frjine = ArithmeticUtils.mul(frbz, order.getFenrun(), 2);
		detail.setBscid1(bsc.getUserid());
		detail.setBscjine1(frjine);
	}

	/**
	 * 设置县办事处分润
	 */
	private void setBsc2Fr(SKOrder order, SKUserFRDetail detail) {
		SKFinPay finpay = sKFinPayService.selectOne(order.getFinpayid());
		if (StringUtils.isBlank(finpay.getXian())) {
			return;
		}
		SKBanShiChu bsc = sKBanShiChuService.getXian(finpay.getShi(), finpay.getXian());
		if (bsc == null) {
			return;
		}
		// 分润标准
		Double frbz = bsc.getFenrun();
		Double frjine = ArithmeticUtils.mul(frbz, order.getFenrun(), 2);
		detail.setBscid2(bsc.getUserid());
		detail.setBscjine2(frjine);
	}

	/**
	 * 设置一级推荐人分润
	 */
	private void setTjr1Fr(SKOrder order, SKUserFRDetail detail) {
		SKBaobei baobei = sKBaobeiService.selectOne(order.getBbid());
		if (baobei.getTjr1() == null) {
			return;
		}
		// 分润标准
		Double tjr1 = 0.1;
		Double frjine = ArithmeticUtils.mul(tjr1, order.getFenrun(), 2);
		detail.setTjr1(baobei.getTjr1());
		detail.setTjrjine1(frjine);
	}
	
	/**
	 * 设置二级推荐人分润
	 */
	private void setTjr2Fr(SKOrder order, SKUserFRDetail detail) {
		SKBaobei baobei = sKBaobeiService.selectOne(order.getBbid());
		if (baobei.getTjr2() == null) {
			return;
		}
		// 分润标准
		Double tjr2 = 0.05;
		Double frjine = ArithmeticUtils.mul(tjr2, order.getFenrun(), 2);
		detail.setTjr2(baobei.getTjr2());
		detail.setTjrjine2(frjine);
	}

	/**
	 * 销售一级分润 类型是119
	 */
	private void saveZhangOne(SKUserFR userFr) {
		if (userFr.getFrjine1() != null && userFr.getFrjine1() > 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(NumberUtils.parseLong(userFr.getOrderid(), null));
			obj.setBusitypeid(119);
			double yue = userFr.getFrjine1();
			obj.setYue(yue);
			obj.setUserid(userFr.getFruserid1());
			SKUser fromuser = sKUserService.selectOne(userFr.getUserid());
			obj.setRemark(fromuser.getNickname() + "的销售分润");
			sKUserZhangService.add(obj);
			sKUserZhangService.commit(obj.getId());
		}
	}

	/**
	 * 销售二级分润 类型是120
	 */
	private void saveZhangTwo(SKUserFR userFr) {
		if (userFr.getFrjine2() != null && userFr.getFrjine2() > 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(NumberUtils.parseLong(userFr.getOrderid(), null));
			obj.setBusitypeid(120);
			double yue = userFr.getFrjine2();
			obj.setYue(yue);
			obj.setUserid(userFr.getFruserid2());
			SKUser fromuser = sKUserService.selectOne(userFr.getFruserid1());
			obj.setRemark(fromuser.getNickname() + "的销售分润");
			sKUserZhangService.add(obj);
			sKUserZhangService.commit(obj.getId());
		}
	}

	/**
	 * 市办事处分润 类型是121
	 */
	private void saveShi(SKUserFR userFr) {
		if (userFr.getBscjine1() != null && userFr.getBscjine1() > 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(NumberUtils.parseLong(userFr.getOrderid(), null));
			obj.setBusitypeid(121);
			double yue = userFr.getBscjine1();
			obj.setYue(yue);
			obj.setUserid(userFr.getBscid1());
			SKUser fromuser = sKUserService.selectOne(userFr.getUserid());
			obj.setRemark(fromuser.getNickname() + "的市办事处分润");
			sKUserZhangService.add(obj);
			sKUserZhangService.commit(obj.getId());
		}
	}

	/**
	 * 县办事处分润 类型是122
	 */
	private void saveXian(SKUserFR userFr) {
		if (userFr.getBscjine2() != null && userFr.getBscjine2() > 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(NumberUtils.parseLong(userFr.getOrderid(), null));
			obj.setBusitypeid(122);
			double yue = userFr.getBscjine2();
			obj.setYue(yue);
			obj.setUserid(userFr.getBscid2());
			SKUser fromuser = sKUserService.selectOne(userFr.getUserid());
			obj.setRemark(fromuser.getNickname() + "的县办事处分润");
			sKUserZhangService.add(obj);
			sKUserZhangService.commit(obj.getId());
		}
	}
	
	
	/**
	 * 一级推荐人润 类型是131
	 */
	private void saveTjr1(SKUserFR userFr) {
		if (userFr.getTjr1() != null && userFr.getTjr1() > 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(NumberUtils.parseLong(userFr.getOrderid(), null));
			obj.setBusitypeid(131);
			double yue = userFr.getTjrjine1();
			obj.setYue(yue);
			obj.setUserid(userFr.getTjr1());
			obj.setRemark("一级推荐人分润");
			sKUserZhangService.add(obj);
			sKUserZhangService.commit(obj.getId());
		}
	}

	/**
	 * 二级推荐人润 类型是132
	 */
	private void saveTjr2(SKUserFR userFr) {
		if (userFr.getTjr2() != null && userFr.getTjr2() > 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(NumberUtils.parseLong(userFr.getOrderid(), null));
			obj.setBusitypeid(132);
			double yue = userFr.getTjrjine2();
			obj.setYue(yue);
			obj.setUserid(userFr.getTjr2());
			obj.setRemark("二级推荐人分润");
			sKUserZhangService.add(obj);
			sKUserZhangService.commit(obj.getId());
		}
	}
	/**
	 * 销售收入 类型是199
	 */
	public synchronized void saveSale(SKUserFR userFr) {
		if (userFr.getStatus() == 0) {
			SKUserZhang obj = new SKUserZhang();
			obj.setBusiid(NumberUtils.parseLong(userFr.getOrderid(), null));
			obj.setBusitypeid(199);
			SKFinPay finpay = sKFinPayService.detail(NumberUtils.parseLong(userFr.getOrderid(), 0L));
			Double fenren = 0D;
			for (SKOrder order : finpay.getOrderList()) {
				if (order.getStatus() == 1) {
					// 该订单,退款还未完成 不进行结算
					return;
				} else if (order.getStatus() == 9) {

				} else {
					fenren = ArithmeticUtils.add(fenren, order.getFenrun());
				}
			}
			fenren = ArithmeticUtils.subAll(fenren, userFr.getFrjine1(), userFr.getFrjine2(), userFr.getBscjine1(), userFr.getBscjine2(), userFr.getSxf());
			obj.setYue(fenren);
			ChangShang changshang = changShangService.selectOne(finpay.getSjid());
			obj.setUserid(changshang.getUserid());
			obj.setRemark("销售收入");
			sKUserZhangService.add(obj);
			sKUserZhangService.commit(obj.getId());
		}
	}

	/**
	 * 销售分润入账
	 * 
	 * @param userid
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	private synchronized void commitZhang(SKUserFR fr) {
		// 进行重新计算后
		SKUserFR newfr = selectOne(fr.getId());
		if (newfr.getStatus() == 0) {
			if ("DD".equals(fr.getPaytype())) {
				SKFinPay finpay = sKFinPayService.selectOne(NumberUtils.parseLong(fr.getOrderid(), 0L));
				finpay.setIsfenrun(1);
				sKFinPayService.update(finpay);
			}

			// 入账
			saveZhangOne(newfr);
			saveZhangTwo(newfr);
			saveShi(newfr);
			saveXian(newfr);
			saveTjr1(newfr);
			saveTjr2(newfr);
			// 销售到账
			saveSale(newfr);
			newfr.setStatus(1);
			update(newfr);
		}
	}

	/**
	 * 销售分润到账
	 * 
	 * @param userid
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public void commitZhang() {
		Map<String, Object> parameters = MapUtils.genMap("endTimeExpire", new Date(), "status", 0);
		List<SKUserFR> list = selectByWhere(parameters);
		if (CollectionUtils.isEmpty(list)) {
			return;
		}
		for (SKUserFR fr : list) {
			commitZhang(fr);
		}
	}

	/**
	 * 获取一个会员的收益情况
	 * 
	 * @param userid
	 */
	public SKUserShouYi getUserShouYi(Long userid) {
		SKUserShouYi shouyi = new SKUserShouYi();
		SKUser user = sKUserService.selectOne(userid);
		shouyi.setId(user.getId());
		shouyi.setNickname(user.getNickname());
		shouyi.setLevelCode(user.getLevelCode());
		shouyi.setUser(user);

		if (user.getRefereeId() != null) {
			SKUser ref = sKUserService.selectOne(user.getRefereeId());
			if (ref != null) {
				shouyi.setRef(ref);
			}
		}

		List<ChangShang> changshangList = changShangService.selectByUserId(userid);
		int isSJ = CollectionUtils.isEmpty(changshangList) ? 0 : 1;
		// 是否商家
		shouyi.setIsSJ(isSJ);

		// 成员、会员
		Long chengyuan = sKUserService.getCountByRefereeId(userid, "0000");
		shouyi.setChengyuan(chengyuan.intValue());
		Long huiyuan = sKUserService.getCountByRefereeId(userid, "0001");
		shouyi.setHuiyuan(huiyuan.intValue());

		Double leiji = sumFrByWhere(null, userid, null, null);
		Date now = new Date();
		Date benyuetime = DateUtils.parse(DateUtils.format(now, "yyyy-MM-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date benzhoutime = DateUtils.getWeekFirstDate();
		Double benyue = sumFrByWhere(null, userid, benyuetime, null);
		Double benzhou = sumFrByWhere(null, userid, benzhoutime, null);

		shouyi.setLeiji(leiji);
		shouyi.setBenyue(benyue);
		shouyi.setBenzhou(benzhou);
		return shouyi;
	}

	/**
	 * 一级分润,包含未到账的
	 * 
	 * @param userid
	 *            产生分润的用户
	 * @return
	 */
	public double sumFr1ByWhere(Long userid, Long fruserid1, Date startTimeStart, Integer status) {
		Map<String, Object> parameters = MapUtils.genMap("fruserid1", fruserid1, "startCreatedTime", startTimeStart, "startStatus", 0);
		if (userid != null) {
			parameters.put("userid", userid);
		}
		if (status != null) {
			parameters.put("status", status);
		}
		Double fr = dao.sumFr1ByWhere(parameters);
		if (fr == null) {
			fr = 0D;
		}
		return fr;
	}

	/**
	 * 二级分润,包含未到账的
	 * 
	 * @param userid
	 *            产生分润的用户
	 * @return
	 */
	public double sumFr2ByWhere(Long userid, Long fruserid2, Date startTimeStart, Integer status) {
		Map<String, Object> parameters = MapUtils.genMap("fruserid2", fruserid2, "startCreatedTime", startTimeStart, "startStatus", 0);
		if (userid != null) {
			parameters.put("fruserid1", userid);
		}
		if (status != null) {
			parameters.put("status", status);
		}
		Double fr = dao.sumFr2ByWhere(parameters);
		if (fr == null) {
			fr = 0D;
		}
		return fr;
	}

	/**
	 * 一级+二级分润
	 * 
	 * @param parameters
	 * @return
	 */
	public double sumFrByWhere(Long userid, Long fruserid, Date startTimeStart, Integer status) {
		Double fr1 = sumFr1ByWhere(userid, fruserid, startTimeStart, status);
		Double fr2 = sumFr2ByWhere(userid, fruserid, startTimeStart, status);
		return ArithmeticUtils.add(fr1, fr2);
	}

	/**
	 * 获取一个会员的收益情况，包含未到账的
	 * 
	 * @param userid
	 */
	public SKUserChanChu getUserChanChu(Long userid, Long fruserid) {
		SKUserChanChu shouyi = new SKUserChanChu();
		SKUser user = sKUserService.selectOne(userid);
		shouyi.setId(user.getId());
		shouyi.setLevelCode(user.getLevelCode());
		// 成员、会员
		Long chengyuan = sKUserService.getCountByRefereeId(userid, "0000");
		shouyi.setChengyuan(chengyuan.intValue());
		Long huiyuan = sKUserService.getCountByRefereeId(userid, "0001");
		shouyi.setHuiyuan(huiyuan.intValue());

		Double leiji = sumFrByWhere(userid, fruserid, null, null);
		Date now = new Date();
		Date benyuetime = DateUtils.parse(DateUtils.format(now, "yyyy-MM-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date benzhoutime = DateUtils.getWeekFirstDate();
		Double benyue = sumFrByWhere(userid, fruserid, benyuetime, null);
		Double benzhou = sumFrByWhere(userid, fruserid, benzhoutime, null);

		shouyi.setLeiji(leiji);
		shouyi.setBenyue(benyue);
		shouyi.setBenzhou(benzhou);
		return shouyi;
	}

	/**
	 * 用户未到账佣金合计
	 * 
	 * @return
	 */
	public Double selectUserYjSum(SKUser user) {
		Double leiji = sumFrByWhere(null, user.getId(), null, 0);
		return leiji;
	}

	/**
	 * 用户未到账佣金列表
	 * 
	 * @return
	 */
	public List<SKUserZhang> selectUserYjList(SKUser user) {
		Long userid = user.getId();
		Map<String, Object> parameters = MapUtils.genMap("userid", userid, "status", 0, "orderby", "createdTime desc");
		List<SKUserFR> list = dao.selectUnionByWhere(parameters);
		List<SKUserZhang> zhanglist = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (SKUserFR fr : list) {
				SKUserZhang zhang = new SKUserZhang();
				zhang.setFr(fr);
				zhang.setStatus(0);
				if (userid.compareTo(fr.getFruserid1()) == 0) {
					zhang.setBusitypeid(119);
					zhang.setYue(fr.getFrjine1());
				} else {
					zhang.setBusitypeid(120);
					zhang.setYue(fr.getFrjine2());
				}
				SKUser syuser = sKUserService.selectOne(fr.getUserid());
				fr.setUserName(syuser.getNickname());
				zhanglist.add(zhang);
			}
		}
		return zhanglist;
	}

}
